package main

import (
	"fmt"
)

func main() {
	// map是引用类型，必须初始化才能调用
	// map的基础格式map[KeyType]ValueType
	map1 := make(map[string]string, 8)
	// 赋值
	map1["key1"] = "value1"
	map1["key2"] = "value2"
	map1["key3"] = "value3"
	map1["key4"] = "value4"
	fmt.Println(map1)

	// map判断某个键是否存在 value, ok := map[key]
	value, ok := map1["key1"]
	if ok {
		fmt.Println(value)
	} else {
		fmt.Println("nil")
	}

	// map的遍历
	for k, v := range map1 {
		fmt.Println(k, v)
	}
	// 只遍历key
	for k := range map1 {
		fmt.Println(k)
	}

	// 删除键值对
	delete(map1, "key1")
	fmt.Println(map1)

}
